//
//  UpdatePasswordViewController.m
//  Lucky28
//
//  Created by zr on 2018/8/6.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "UpdatePasswordViewController.h"

@interface UpdatePasswordViewController ()

@property (nonatomic, weak)UIView * backView;
@property (nonatomic, strong)UITextField * oldPasswordText;
@property (nonatomic, strong)UITextField * passwordText;
@property (nonatomic, strong)UITextField * repasswordText;
@property (nonatomic, weak)UIButton * loginButton;

@end

@implementation UpdatePasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupNav];
    [self setupUI];
}


- (void)setupNav{
    
    self.title = @"修改密码";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    //    backButton.frame = CGRectMake(0, 0, 20 * SCREENH6, 20 * SCREENH6);
    backButton.size = CGSizeMake(20 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
    
}



- (void)setupUI{
    
    self.view.backgroundColor = ColorWithRGB(0xe8e8e8);
    
    UIView * backView = [UIView new];
    
    backView.backgroundColor = [UIColor whiteColor];
    backView.userInteractionEnabled = YES;
    
    self.backView = backView;
    
    [self.view addSubview:self.backView];
    
    
    self.oldPasswordText = [self setupTextFiledWith:@"请输入您的旧密码"];
    
    [self.backView addSubview:self.oldPasswordText];
    
    
    self.passwordText = [self setupTextFiledWith:@"请输入您的新密码"];
    
    [self.backView addSubview:self.passwordText];
    
    
    self.repasswordText = [self setupTextFiledWith:@"请再次输入您的新密码"];
    
    [self.backView addSubview:self.repasswordText];
    
    
    UIButton * loginButton = [[UIButton alloc] init];
    
    [loginButton setTitle:@"提交" forState:UIControlStateNormal];
    loginButton.titleLabel.font = [UIFont boldSystemFontOfSize:16 * SCREENH6];
//    [loginButton setBackgroundImage:[UIImage imageNamed:@"Button"] forState:UIControlStateNormal];
    [loginButton setBackgroundColor:ColorWithRGB(0xD61E4D)];
    
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(didLoginButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.loginButton = loginButton;
    
    [self.view addSubview:self.loginButton];
}

-(void)viewDidLayoutSubviews{
    
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.mas_equalTo(self.view);
        make.height.mas_equalTo(180 * SCREENH6);
    }];
    
    [self.oldPasswordText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backView).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self.backView).offset(-15 * SCREENH6);
        make.height.mas_equalTo(45 * SCREENH6);
        make.top.mas_equalTo(self.view).offset(10 * SCREENH6);
    }];
    
    [self.passwordText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.oldPasswordText);
        make.top.mas_equalTo(self.oldPasswordText.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.repasswordText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.passwordText);
        make.top.mas_equalTo(self.passwordText.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(45 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-45 * SCREENH6);
        make.height.mas_equalTo(50 * SCREENH6);
        make.top.mas_equalTo(self.backView.mas_bottom).offset(30 * SCREENH6);
    }];
}

- (UITextField *)setupTextFiledWith:(NSString *)string{
    
    UITextField * textFiled = [[UITextField alloc] init];
    
    textFiled.borderStyle = UITextBorderStyleNone;
    textFiled.backgroundColor = [UIColor whiteColor];
    //设置光标颜色
    textFiled.tintColor = [UIColor blackColor];
    //修改占位符文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    textFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:string attributes:attrs];
    textFiled.textColor = [UIColor blackColor];
    textFiled.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    textFiled.secureTextEntry = YES;
    
    
    return textFiled;
}

- (void)didLoginButton{
    
    [self.oldPasswordText resignFirstResponder];
    [self.passwordText resignFirstResponder];
    [self.repasswordText resignFirstResponder];
    
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    
    NSString * token = [userDefaults objectForKey:@"token"];
    
    NSDictionary * dict = @{@"token":token,@"oldpassword":self.oldPasswordText.text,@"password":self.repasswordText.text};
    
    [[NetworkTool sharedTools] POST:@"/api/member/pwd" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        int status = [responseObject[@"code"] intValue];
        
        if (status == 200) {
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:@"恭喜你修改密码成功"
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      //响应事件
                                                                      [self.navigationController popViewControllerAnimated:YES];
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }else{
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:responseObject[@"msg"]
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      //响应事件
                                                                      [self.navigationController popViewControllerAnimated:YES];
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.oldPasswordText resignFirstResponder];
    [self.passwordText resignFirstResponder];
    [self.repasswordText resignFirstResponder];
}

- (void)backToView {
   
    [self.navigationController popViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
